package com.mm.dss.demo;

import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.LocationDisplayManager;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.event.OnZoomListener;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleLineSymbol;
import com.mm.dss.demo.base.BaseActivity;

import static com.mm.dss.demo.R.id.camera;
import static com.mm.dss.demo.R.id.swipregresh;
import static com.mm.dss.demo.R.id.txt_title;

public class MapActivity extends BaseActivity {
    MapView mapView;
    GraphicsLayer graphicsLayer;
    private Graphic graphic;
    private String url="http://60.208.94.143:6080/arcgis/rest/services/LW/LWDHPT/MapServer/";
    private String url2="http://60.216.249.11:6080/arcgis/rest/services/QCMap/MapServer";
    private  String dturl="http://60.216.249.11:6080/arcgis/rest/services/LW/LWDHPT/MapServer";//莱芜动态图层
    private double lat = -1;
    private double lng = -1;
    private TextView txt_title;
    private RelativeLayout layout_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        initview();//
        ArcGISRuntime.setClientId("1eFHW78avlnRUPHm");
        ArcGISTiledMapServiceLayer gg = new//替换底图
                ArcGISTiledMapServiceLayer
                ("http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");
        //http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer
        //影像 http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer
        mapView.addLayer(gg);
        ArcGISDynamicMapServiceLayer TT1 = new//添加新的图层动态图层
                ArcGISDynamicMapServiceLayer(url,new int[0]);
        mapView.addLayer(TT1);
        dingweilocation();//定位
        mapmethod();
    }

    private void initview() {
        mapView= (MapView) findViewById(R.id.map);
        txt_title= (TextView) findViewById(R.id.txt_title);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        txt_title.setText("地图浏览");
        layout_back.setVisibility(View.VISIBLE);
    }

    private void mapmethod() {
        ArcGISDynamicMapServiceLayer TT = new//添加新的图层动态图层
                ArcGISDynamicMapServiceLayer(dturl);
        mapView.addLayer(TT);
        mapView.setOnZoomListener(new OnZoomListener() {
            //缩放监听
            @Override
            public void preAction(float v, float v1, double v2) {
                Log.i("缩放监听",v+"/"+v1+"/"+v2+"/"+mapView.getScale());
            }
            @Override
            public void postAction(float v, float v1, double v2) {

            }
        });
        mapView.setOnStatusChangedListener(new OnStatusChangedListener() {
            @Override
            public void onStatusChanged(Object o, STATUS status) {
                switch (status) {
                    case INITIALIZED:
                        break;
                    case INITIALIZATION_FAILED:
                     /*   show("初始化失败！请检查网络后重试。");
                        ProgressDialogUtil.Dismiss();*/
                        break;
                    case LAYER_LOADED:
                        Log.i("查11","执行了");

                        break;
                    case LAYER_LOADING_FAILED:
//                        show("图层加载失败！请检查网络后重试。");
//                        ProgressDialogUtil.Dismiss();
                        break;
                }

                if (lat != -1 & lng != -1) {
                    mapView.centerAt(lat, lng, true);
                }
                mapView.setScale(93485.23994712761);//比例尺  99240.8989612349
            }
        });
    }


    private void dingweilocation() {
        LocationDisplayManager locationDisplayManager = mapView.getLocationDisplayManager();
        if (locationDisplayManager==null){

        }else {
            locationDisplayManager.setLocationListener(new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    lat = location.getLatitude() + 0.000498;//纬度
                    lng = location.getLongitude() - 0.006157;//经度
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            });
            locationDisplayManager.start();

        }
    }

    public  void  onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
            case R.id.location:
                dingweilocation();
                if (lat != -1 & lng != -1) {
                    mapView.centerAt(lat, lng, true);
                    mapView.setScale(93485.23994712761);
                }
                break;
            case R.id.jia:
                mapView.zoomin();
                break;
            case R.id.jian:
                mapView.zoomout();
                break;
        }
    }
}
